<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['stores']=pdo_getall('jy_cxjxiche_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=trim($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(isset($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','name','mobile','busy','status','stop','gender','createtime','store_id','idcard','bx','address','cid');
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_cxjxiche_worker',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['createtime']=date('Y-m-d H:I:s',$v['createtime']);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'师傅管理'),
			array('op'=>'shenhe','title'=>'待审核师傅'),
			array('op'=>'edit','title'=>'添加师傅'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：师傅不能接自己的单！！','list'=>array(
			array('name'=>'id','title'=>'id','type'=>'string'),
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'idcard','title'=>'身份证号','type'=>'string'),
			array('name'=>'store_id','title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'address','title'=>'现住地址','type'=>'string'),
			array('name'=>'gender','title'=>'性别','type'=>'radio','options'=>'gender'),
			array('name'=>'status','title'=>'审核状态','type'=>'radio','options'=>'status'),
			array('name'=>'stop','title'=>'是否禁号','type'=>'switch','set'=>'set'),
			array('name'=>'busy','title'=>'工作状态','type'=>'radio','options'=>'online'),	
		)),
		'search'=>array(
			array('name'=>'name','width'=>15,'title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','width'=>15,'title'=>'手机号','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	$return['status']=array('1'=>'已审核','0'=>'待审核','2'=>'已驳回');
	$return['stop']=array('1'=>'是','0'=>'否');
	$return['online']=$this->online;
	$return['gender']=array('1'=>'男','2'=>'女');
	exi($return);
}
if($_RQ['op']=='shenhe'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>0);
	$fields=array('id','name','mobile','status','stop','gender','createtime','store_id','idcard','bx','address','cid');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_cxjxiche_worker',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['createtime']=date('Y-m-d H:m:s',$v['createtime']);
		}
	$return['status']=array('0'=>'待审核','1'=>'已审核','2'=>'已驳回');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'师傅管理'),
			array('op'=>'shenhe','title'=>'待审核师傅'),
			array('op'=>'edit','title'=>'添加师傅'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'id','title'=>'id','type'=>'string'),
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'idcard','title'=>'身份证号','type'=>'string'),
			array('name'=>'store_id','title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'address','title'=>'现住地址','type'=>'string'),
			array('name'=>'gender','title'=>'性别','type'=>'radio','options'=>'gender'),
			array('name'=>'status','title'=>'审核状态','type'=>'radio','options'=>'status'),
			
				
		),
		'action'=>array(	
			array('type'=>'href','op'=>'yes','title'=>'通过'),
			array('type'=>'href','op'=>'no','title'=>'驳回'),
		),
		),
		
	);
	$return['gender']=array('1'=>'男','2'=>'女');
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='yes'){
		$id=intval($_RQ['id']);
		if($id){
			pdo_update('jy_cxjxiche_worker',array('status'=>1),array('id'=>$id));
			exi('已通过','','/worker/');
		}else{
			exi('请先选择师傅','','/worker/');
		}
}
if($_RQ['op']=='no'){
		$id=intval($_RQ['id']);
		if($id){
			pdo_update('jy_cxjxiche_worker',array('status'=>2),array('id'=>$id));
			exi('已驳回','','/worker/');
		}else{
			exi('请先选择师傅','','/worker/');
		}
}
	
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_cxjxiche_worker',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['latlng']=$item['latlng']?json_decode($item['latlng'],true):array();
    $return['item']=$item;
	$return['status']=array('0'=>'待审核','1'=>'已审核','2'=>'已驳回');
	$return['stop']=array('1'=>'是','0'=>'否');
	$return['gender']=array('1'=>'男','2'=>'女');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'师傅设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'师傅管理'),
			array('op'=>'edit','title'=>'添加师傅')
		),
		'edit'=>array(	
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'idcard','title'=>'身份证号','type'=>'string'),
			array('name'=>'store_id','title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'avatar','title'=>'头像','type'=>'image'),
			array('name'=>'address','title'=>'现住地址','type'=>'string'),
			array('name'=>'latlng','title'=>'坐标','type'=>'lat_lng'),
			array('name'=>'mid','title'=>'绑定用户','type'=>'choose_title','action'=>'core/member.get_member/'),	
			array('name'=>'gender','title'=>'性别','type'=>'radio','options'=>'gender'),
			array('name'=>'busy','title'=>'上线?','type'=>'switch'),
			array('name'=>'status','title'=>'审核状态','type'=>'radio','options'=>'status'),
			array('name'=>'stop','title'=>'是否禁号','type'=>'switch'),				
		)
	);
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$postdata['latlng']=ijson_encode($postdata['latlng']);
	$data=array_fields(array('name','avatar','busy','latlng','cid','mobile','stop','gender','mid','status','address','store_id','idcard','bx'),$postdata);
	
	if(!$data['name']){
		exi('请填写姓名','error');
	}
	if(!intval($data['mobile']) || strlen($data['mobile'])!=11){
		exi('请填写正确手机号','error');
	}
	if(!intval($data['store_id'])){
		exi('请选择所属门店！','error');
	}
	$id=intval($postdata['id']);
	$data['uplat_time']=TIMESTAMP;
	if($id){
		pdo_update('jy_cxjxiche_worker',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_cxjxiche_worker',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['stop'])){
		$update['stop']=intval($post['stop']);
	}
	if($update){
		pdo_update('jy_cxjxiche_worker',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_cxjxiche_worker',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_cxjxiche_worker',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>